local prefab_id = 'fx_webbert_skitter_heallaser'
local assets = {
    Asset('ANIM', 'anim/fx_webbert_skitter_heallaser.zip'),
}

local prefabs = {}

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()

    inst.entity:AddNetwork()
    -- inst.entity:AddSoundEmitter()
    -- MakeInventoryPhysics(inst)

    inst.AnimState:SetBank('fx_webbert_skitter_heallaser')
    inst.AnimState:SetBuild('fx_webbert_skitter_heallaser')
    inst.AnimState:PlayAnimation('idle',true)
    -- inst.AnimState:SetBank('fx_webbert_skitter_heallaser')
    -- inst.AnimState:SetBuild('fx_webbert_skitter_heallaser')
    -- inst.AnimState:PlayAnimation('idle',true)
    -- inst.AnimState:SetBank('fx_webbert_skitter_heallaser')
    -- inst.AnimState:SetBuild('fx_webbert_skitter_heallaser')
    -- inst.AnimState:PlayAnimation('idle',true)
    -- inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
    -- inst.AnimState:SetLightOverride(1)
    -- inst.AnimState:SetDeltaTimeMultiplier(0.2)


    -- inst.AnimState:PushAnimation('idle_loop',true)
    -- inst.AnimState:PlayAnimation('out_idle',true)

    -- inst.AnimState:PushAnimation('loop',true)

    -- inst.AnimState:SetDeltaTimeMultiplier(.6)

    inst.AnimState:SetOrientation(ANIM_ORIENTATION.OnGround)
    -- inst.AnimState:SetOrientation(ANIM_ORIENTATION.Default)
    inst.AnimState:SetLayer(LAYER_WORLD_BACKGROUND)
    inst.AnimState:SetSortOrder(1)

    -- inst.AnimState:SetMultColour(1,1,1,0.7)

    inst:AddTag("NOCLICK")
    inst:AddTag("NOBLOCK")
    inst:AddTag("FX")

    -- inst.Transform:SetScale(1.5,1.5,1.5)

    if not TheWorld.ismastersim then
        return inst
    end

    inst.persists = false

    inst._fx_webbert_skitter_heallaser_heal_val = 3
    inst._fx_webbert_skitter_heallaser_follow_source = nil
    inst._fx_webbert_skitter_heallaser_follow_player = nil
    ---comment
    ---@param this ent
    ---@param player ent
    inst.fn_fx_webbert_skitter_heallaser_follow = function (this, source, player,heal_val)
        -- inst.entity:SetParent(source.entity)
        local x,_,z = source:GetPosition():Get()
        inst.Transform:SetPosition(x,0,z)
        inst._fx_webbert_skitter_heallaser_follow_source = source
        inst._fx_webbert_skitter_heallaser_follow_player = player
        inst._fx_webbert_skitter_heallaser_heal_val = heal_val
    end

    local heal = 3
    local heal_interval_frames = 40
    local _laser_length = 2
    local h = 0
    inst:DoPeriodicTask(0,function ()
        h = (h + 1)%heal_interval_frames
        local player = inst._fx_webbert_skitter_heallaser_follow_player
        local source = inst._fx_webbert_skitter_heallaser_follow_source
        if inst and inst:IsValid() and player and player:IsValid() and source and source:IsValid() and player.components.health and not player:HasTag('playerghost') and player.components.health:GetPercentWithPenalty() < .8 and source.components.health and not source.components.health:IsDead() then
            local p_x,_,p_z = inst._fx_webbert_skitter_heallaser_follow_player:GetPosition():Get()
            local x,_,z = inst._fx_webbert_skitter_heallaser_follow_source:GetPosition():Get()
            local dist = COORDS_webber_the_trainer:calcDist(p_x,p_z,x,z,true)
            local scale = dist / _laser_length
            inst.Transform:SetPosition(x,0,z)
            inst:ForceFacePoint(p_x,1,p_z)
            inst.AnimState:SetScale(scale,1)
            if h == 0 then
                player.components.health:DoDelta(inst._fx_webbert_skitter_heallaser_heal_val,true)
                source.components.health:DoDelta(-inst._fx_webbert_skitter_heallaser_heal_val)
            end
        else
            if inst and inst:IsValid() then
                inst:Remove()
            end
        end
    end,0)

    -- inst:ListenForEvent("animover", inst.Remove)

    return inst
end

return Prefab(prefab_id, fn, assets, prefabs)
